package com.idrem.views.visit;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.idrem.R;
import com.idrem.common.Common;
import com.idrem.common.MySharedPreferences;

import java.io.File;
import java.util.List;

public class CustomerAdapter extends ImageListViewAdapter {
	private Context context;
	private String[][] custData;
	private Drawable drawable;
	private Double xDouble;
	private Double yDouble;

	public CustomerAdapter(Context paramContext, List paramList) {
		super(paramContext, paramList);
	}

	public CustomerAdapter(Context paramContext, String[][] paramArrayOfString, double paramDouble1, double paramDouble2) {
		super(paramContext, null);
		this.custData = paramArrayOfString;
		this.context = paramContext;
		this.drawable = paramContext.getResources().getDrawable(R.drawable.imageloading);
		this.xDouble = Double.valueOf(paramDouble1);
		this.yDouble = Double.valueOf(paramDouble2);
	}

	public Bitmap doLoadImage(String paramString) {
		BitmapFactory.Options localOptions = new BitmapFactory.Options();
		localOptions.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(paramString, localOptions);
		int i = (int) (localOptions.outHeight / 78.0F);
		if (i <= 0)
			i = 1;
		localOptions.inJustDecodeBounds = false;
		localOptions.inSampleSize = i;
		Bitmap localBitmap = BitmapFactory.decodeFile(paramString, localOptions);
		if (localBitmap == null) {
			File localFile = new File(paramString);
			if ("error".equals(Common.downloadFileByName(this.context, localFile.getName(), localFile)))
				localBitmap = null;
		}
		return localBitmap;
	}

	public int getCount() {
		return this.custData.length;
	}

	public String getDefaultImage() {
		return null;
	}

	public String[] getImage(int paramInt, Object paramObject) {
		String str = Common.getImageFolderPath(this.context) + "customer/" + MySharedPreferences.getInstance(this.context).getDmsId() + this.custData[paramInt][0] + ".jpg";
		String[] arrayOfString = new String[1];
		arrayOfString[0] = str;
		return arrayOfString;
	}

	public Object getItem(int paramInt) {
		return null;
	}

	public View getView(int paramInt, View paramView, ViewGroup paramViewGroup, Object paramObject) {
		View localView = LayoutInflater.from(this.context).inflate(R.layout.visitlistitem_wai, null);
		TextView localTextView1 = (TextView) localView.findViewById(R.id.visitmain_wai_cust_distance);
		TextView localTextView2 = (TextView) localView.findViewById(R.id.visitmain_wai_cust_code);
		TextView localTextView3 = (TextView) localView.findViewById(R.id.visitmain_wai_cust_name);
		TextView localTextView4 = (TextView) localView.findViewById(R.id.visitmain_wai_cust_class);
		TextView localTextView5 = (TextView) localView.findViewById(R.id.visitmain_wai_cust_area);
		TextView localTextView6 = (TextView) localView.findViewById(R.id.visitmain_wai_cust_status);
		ImageView localImageView = (ImageView) localView.findViewById(R.id.visitmain_wai_cust_pic);
		localTextView2.setText(this.custData[paramInt][0]);
		localTextView3.setText(this.custData[paramInt][1]);
		localTextView5.setText(this.custData[paramInt][6]);
		localTextView4.setText(this.custData[paramInt][7]);
		localTextView6.setText(this.custData[paramInt][2]);

		if (stringToDouble(this.custData[paramInt][3]) == 0.0D) {
			localTextView1.setText("无坐标");
		}
		if ((this.xDouble.doubleValue() == 0.0D) && (this.yDouble.doubleValue() == 0.0D))
			localTextView1.setText("无效");

		int i = (int) (1000.0D * Common.getDistatce(this.xDouble.doubleValue(), stringToDouble(this.custData[paramInt][3]), this.yDouble.doubleValue(), stringToDouble(this.custData[paramInt][4])));
		if (i >= 1000) {
			String str2 = "" + i / 1000.0D;
			localTextView1.setText(str2.substring(0, 2 + str2.indexOf(".")) + "公里");
		} else
			localTextView1.setText(i + "米");
		
		if (!this.custData[paramInt][2].equals("已拜访"))
			localTextView6.setTextColor(Color.rgb(0, 144, 204));
		
		
		String str1 = Common.getImageFolderPath(this.context) + "customer/" + MySharedPreferences.getInstance(this.context).getDmsId() + this.custData[paramInt][0] + ".jpg";
		File localFile = new File(str1);
		boolean bool = localFile.exists();
		
		if (!isSmoothMode())
			localImageView.setImageResource(R.drawable.imageloading);
		
		Bitmap localBitmap = loadDrawable(getImage(paramInt, paramObject)[0]);
		if (localBitmap != null) {
			localImageView.setImageBitmap(Common.makeImg(50, 50, localBitmap));
		}
		localImageView.setImageResource(R.drawable.noimage);
		
		// localImageView.setOnClickListener(new CustomerAdapter(paramContext, paramList);
		return localView;
	}

	public void setData(String[][] paramArrayOfString) {
		this.custData = paramArrayOfString;
		notifyDataSetChanged();
	}

	public void setData(String[][] paramArrayOfString, String paramString) {
		this.custData = paramArrayOfString;
		loadImage(paramString);
		notifyDataSetChanged();
	}

	public void setXY(Double paramDouble1, Double paramDouble2) {
		this.xDouble = paramDouble1;
		this.yDouble = paramDouble2;
		notifyDataSetChanged();
	}

	double stringToDouble(String paramString) {
		double d1 = 0.0D;
		try {
			double d2 = Double.parseDouble(paramString);
			d1 = d2;
			return d1;
		} catch (Exception localException) {
			return d1;
		}
	}
}